<template>
  <div>
    <div class="demo-upload-list" :key="index" v-for="(item,index) in uploadList">
      <template v-if="item.status === 'finished'">
        <img :src="item.url">
        <div class="demo-upload-list-cover">
          <Icon type="ios-eye-outline" @click.native="handleView(item.url)"></Icon>
          <Icon v-show="!disabled" type="ios-trash-outline" @click.native="handleRemove(item)"></Icon>
        </div>
      </template>
      <template v-else>
        <Progress v-if="item.showProgress" :percent="item.percentage" hide-info></Progress>
      </template>
    </div>
    <Upload v-show="isShow" ref="upload" :show-upload-list="false" :default-file-list="defaultList" :on-success="handleSuccess"
            :on-error="handleError" :format="['jpg','jpeg','png']" :max-size="2048" :on-format-error="handleFormatError"
            :on-exceeded-size="handleMaxSize" type="drag" action="/pub/configure/uploadFile" style="display: inline-block;width:146px;">
      <div style="width: 146px;height:146px;padding-top:50px;">
        <Icon type="ios-camera" size="20"></Icon>
        <br>
        <span style="color:red;">请上传2MB以下的图片</span>
      </div>
    </Upload>
    <Modal title="View Image" v-model="visible">
      <img :src="imgName" v-if="visible" style="width: 100%">
    </Modal>
  </div>
</template>
<script>
export default {
  props: ["value",'isDefault','disabled'],
  data() {
    return {
      defaultList: this.value
        ? [
            {
              url: this.value
            }
          ]
        : this.defaultImage?[{url:'/golf/img/2020/08/27/3879145799255040.png'}]:[],
      imgName: "",
      isShow: true,
      visible: false,
      uploadList: [],
    };
  },
  watch: {
    value: function(newVal, oldVal) {
      let self = this;
      if (newVal && newVal !== oldVal) {
        self.uploadList = [
          {
            status: "finished",
            url: newVal
          }
        ];
      }else {
        self.uploadList = [];
      }
      self.isShowIcon();
    }
  },
  methods: {
    handleView(name) {
      this.imgName = name;
      this.visible = true;
    },
    isShowIcon() {
      this.isShow = this.uploadList.length === 0;
    },
    handleRemove(file) {
      this.uploadList = [];
      this.$emit("upload", null);
      this.isShowIcon();
    },
    handleSuccess(res, file) {
      if (res.result.retCode === 0) {
        file.url = res.data.fileName;
        this.uploadList = [{url:file.url,status: "finished"}];
        this.$emit("upload", {url:file.url,status: "finished"});
      } else {
        this.$Message.error(res.result.retMessage);
      }
      this.isShowIcon();
    },
    handleError(error, file) {
      this.$Message.error(file.message);
    },
    handleFormatError(file) {
      this.$Notice.warning({
        title: "文件格式不正确",
        desc: file.name + "文件格式不正确，请选择JPG或PNG。"
      });
    },
    handleMaxSize(file) {
      this.$Notice.warning({
        title: "超过文件大小限制",
        desc: "文件  " + file.name + " 太大，不能超过2M。"
      });
    }
  },
  mounted() {
    this.uploadList = this.$refs.upload.fileList;
    this.isShowIcon();
  }
};
</script>
<style>
.modal-img-view .ivu-modal-mask {
  z-index: 11111 !important;
}
.modal-img-view .ivu-modal-wrap {
  z-index: 11111 !important;
}
.demo-upload-list {
  display: inline-block;
  height: 146px;
  text-align: center;
  line-height: 146px;
  border: 1px solid transparent;
  border-radius: 4px;
  overflow: hidden;
  background: #fff;
  position: relative;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
  margin-right: 4px;
}
.demo-upload-list img {
  height: 146px;
  width: 146px;
}
.demo-upload-list-cover {
  display: none;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.6);
}
.demo-upload-list:hover .demo-upload-list-cover {
  display: block;
}
.demo-upload-list-cover i {
  color: #fff;
  font-size: 20px;
  cursor: pointer;
  margin: 0 2px;
}
</style>
